home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / lu.cc < prev    next >
C/C++ Source or Header  |  1996-11-03  |  3KB  |  141 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include "CmplxLU.h"
  28. #include "dbleLU.h"
  29.  
  30. #include "defun-dld.h"
  31. #include "error.h"
  32. #include "gripes.h"
  33. #include "help.h"
  34. #include "oct-obj.h"
  35. #include "utils.h"
  36.  
  37. DEFUN_DLD (lu, args, nargout,
  38.   "[L, U, P] = lu (A): LU factorization")
  39. {
  40.   octave_value_list retval;
  41.  
  42.   int nargin = args.length ();
  43.  
  44.   if (nargin != 1 || nargout > 3)
  45.     {
  46.       print_usage ("lu");
  47.       return retval;
  48.     }
  49.  
  50.   octave_value arg = args(0);
  51.  
  52.   int nr = arg.rows ();
  53.   int nc = arg.columns ();
  54.  
  55.   int arg_is_empty = empty_arg ("lu", nr, nc);
  56.  
  57.   if (arg_is_empty < 0)
  58.     return retval;
  59.   else if (arg_is_empty > 0)
  60.     return octave_value_list (3, Matrix ());
  61.  
  62.   if (nr != nc)
  63.     {
  64.       gripe_square_matrix_required ("lu");
  65.       return retval;
  66.     }
  67.  
  68.   if (arg.is_real_type ())
  69.     {
  70.       Matrix m = arg.matrix_value ();
  71.  
  72.       if (! error_state)
  73.     {
  74.       LU fact (m);
  75.  
  76.       switch (nargout)
  77.         {
  78.         case 0:
  79.         case 1:
  80.         case 2:
  81.           {
  82.         Matrix P = fact.P ();
  83.         Matrix L = P.transpose () * fact.L ();
  84.         retval(1) = fact.U ();
  85.         retval(0) = L;
  86.           }
  87.           break;
  88.  
  89.         case 3:
  90.         default:
  91.           retval(2) = fact.P ();
  92.           retval(1) = fact.U ();
  93.           retval(0) = fact.L ();
  94.           break;
  95.         }
  96.     }
  97.     }
  98.   else if (arg.is_complex_type ())
  99.     {
  100.       ComplexMatrix m = arg.complex_matrix_value ();
  101.  
  102.       if (! error_state)
  103.     {
  104.       ComplexLU fact (m);
  105.  
  106.       switch (nargout)
  107.         {
  108.         case 0:
  109.         case 1:
  110.         case 2:
  111.           {
  112.         ComplexMatrix P = fact.P ();
  113.         ComplexMatrix L = P.transpose () * fact.L ();
  114.         retval(1) = fact.U ();
  115.         retval(0) = L;
  116.           }
  117.           break;
  118.  
  119.         case 3:
  120.         default:
  121.           retval(2) = fact.P ();
  122.           retval(1) = fact.U ();
  123.           retval(0) = fact.L ();
  124.           break;
  125.         }
  126.     }
  127.     }
  128.   else
  129.     {
  130.       gripe_wrong_type_arg ("lu", arg);
  131.     }
  132.  
  133.   return retval;
  134. }
  135.  
  136. /*
  137. ;;; Local Variables: ***
  138. ;;; mode: C++ ***
  139. ;;; End: ***
  140. */
  141.